package com.njbw.user.designPattern.filterPattern;

import java.util.List;
import java.util.stream.Collectors;

public class FilterPatternDemo {

    private List<ArticleFilterEnum> articleFilterEnumList;

    private List<IArticleFilter> articleFilterList;

    private void init() {
        this.articleFilterList = ArticleFilterFactory.createArticleFilter(articleFilterEnumList);
    }

    public void doFilter(List<Article> articleList) {
        List<Article> collect = articleList.stream().filter(article -> {
            ArticleContext articleContext = new ArticleContext();
            articleContext.setArticle(article);
            return doFilter(articleContext);
        }).collect(Collectors.toList());
    }

    private boolean doFilter(ArticleContext articleContext) {
        for (IArticleFilter articleFilter : articleFilterList) {
            if (articleFilter.doFilter(articleContext)) {
                return false;
            }
        }
        return true;
    }
}
